# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Xin Sun <sun.simpson@gmail.com>

_realname=pyside-tools
pkgbase=mingw-w64-${_realname}-qt4
pkgname=(${MINGW_PACKAGE_PREFIX}-${_realname}-common-qt4 ${MINGW_PACKAGE_PREFIX}-python{2,3}-${_realname}-qt4)
pkgver=1.2.2
pkgrel=3
pkgdesc="Python bindings for Qt4 (mingw-w64)"
arch=('any')
url="https://pyside.github.io/docs/pyside/"
license=("LGPL")
source=(https://pypi.python.org/packages/source/P/PySide/PySide-${pkgver}.tar.gz)
sha256sums=('53129fd85e133ef630144c0598d25c451eab72019cdcb1012f2aec773a3f25be')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-python2-pyside-qt4"
             "${MINGW_PACKAGE_PREFIX}-python3-pyside-qt4"
             "${MINGW_PACKAGE_PREFIX}-qt4")
options=('staticlibs' 'strip')

prepare() {
  cd ${srcdir}/PySide-${pkgver}
}

build() {
  for pyv in python2 python3; do
    local _conf=
    if [ "${pyv}" = "python2" ]; then
      _conf="-DPYTHON_SUFFIX=-python2.7 -DPYTHON_BASENAME=-python2.7"
    fi
    [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
    cp -rf PySide-${pkgver} build-${MINGW_CHOST}-${pyv}
    pushd ${srcdir}/build-${MINGW_CHOST}-${pyv}/sources/pyside-tools > /dev/null

    export MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX"
    ${MINGW_PREFIX}/bin/cmake \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      ${_conf} \
      -DCMAKE_BUILD_TYPE=RELEASE \

    make

    popd > /dev/null
  done

}

package_pyside-tools-common() {
  pkgdesc="PySide lupdate, rcc, and uic development tools (Common Files) (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-qt4")

  cd ${srcdir}/build-${MINGW_CHOST}-python3/sources/pyside-tools
  make DESTDIR=${pkgdir} install

  rm -rf "${pkgdir}${MINGW_PREFIX}"/lib
  rm -f "${pkgdir}${MINGW_PREFIX}"/bin/pyside-uic
  rm -f "${pkgdir}${MINGW_PREFIX}"/share/man/man1/pyside-uic*
}

package_python2-pyside-tools() {
  pkgdesc="PySide lupdate, rcc, and uic development tools (for Python 2) (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-pyside-tools-common-qt4"
           "${MINGW_PACKAGE_PREFIX}-python2-pyside-qt4")

  cd ${srcdir}/build-${MINGW_CHOST}-python2/sources/pyside-tools
  make DESTDIR=${pkgdir} install

  find "${pkgdir}${MINGW_PREFIX}" -type f -exec \
  sed -i -e '1 s|^#! *${MINGW_PREFIX}/bin/python[0-9.]*|#!${MINGW_PREFIX}/bin/python2|' \
         -e '1 s|^#! *${MINGW_PREFIX}/bin/env python[0-9.]*|#!${MINGW_PREFIX}/bin/env python2|' {} +

  mv "${pkgdir}${MINGW_PREFIX}"/bin/{,python2-}pyside-uic
  mv "${pkgdir}${MINGW_PREFIX}"/share/man/man1/{,python2-}pyside-uic.1

  rm -rf "${pkgdir}${MINGW_PREFIX}"/lib/python2*/site-packages/pysideuic/{port_v3,pyside-uic.1}
  rm -f "${pkgdir}${MINGW_PREFIX}"/bin/pyside-{lupdate,rcc}
  rm -f "${pkgdir}${MINGW_PREFIX}"/share/man/man1/pyside-{lupdate,rcc}*

  ${MINGW_PREFIX}/bin/python2 -m compileall "${pkgdir}${MINGW_PREFIX}"/lib/python2*/site-packages/pysideuic/
  ${MINGW_PREFIX}/bin/python2 -O -m compileall "${pkgdir}${MINGW_PREFIX}"/lib/python2*/site-packages/pysideuic/
}

package_python3-pyside-tools() {
  pkgdesc="PySide lupdate, rcc, and uic development tools (for Python 3) (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-pyside-tools-common-qt4"
           "${MINGW_PACKAGE_PREFIX}-python3-pyside-qt4")

  cd ${srcdir}/build-${MINGW_CHOST}-python3/sources/pyside-tools
  make DESTDIR=${pkgdir} install

  rm -rf "${pkgdir}${MINGW_PREFIX}"/lib/python3*/site-packages/pysideuic/{port_v2,pyside-uic.1}
  rm -f "${pkgdir}${MINGW_PREFIX}"/bin/pyside-{lupdate,rcc}
  rm -f "${pkgdir}${MINGW_PREFIX}"/share/man/man1/pyside-{lupdate,rcc}*

  ${MINGW_PREFIX}/bin/python3 -m compileall "${pkgdir}${MINGW_PREFIX}"/lib/python3*/site-packages/pysideuic/
  ${MINGW_PREFIX}/bin/python3 -O -m compileall "${pkgdir}${MINGW_PREFIX}"/lib/python3*/site-packages/pysideuic/
}

# Wrappers
package_mingw-w64-i686-pyside-tools-common-qt4() {
  package_pyside-tools-common
}

package_mingw-w64-i686-python2-pyside-tools-qt4() {
  package_python2-pyside-tools
}

package_mingw-w64-i686-python3-pyside-tools-qt4() {
  package_python3-pyside-tools
}

package_mingw-w64-x86_64-pyside-tools-common-qt4() {
  package_pyside-tools-common
}

package_mingw-w64-x86_64-python2-pyside-tools-qt4() {
  package_python2-pyside-tools
}

package_mingw-w64-x86_64-python3-pyside-tools-qt4() {
  package_python3-pyside-tools
}
